﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CaiyinSoft.NetGather.Protocol;

namespace CaiyinSoft.NetGather.NetGatherLib.Publisher
{
    [Serializable]
    public class HTMLPublisherBase : IHTMLPublisher
    {

        public virtual void Publish(Dictionary<int, string> HTMLList, IHTMLAnalyzer HTMLAnalyzer)
        { 
        }

        public virtual void PublishListData(Dictionary<int, string> HTMLList, IHTMLAnalyzer HTMLAnalyzer)
        {
        }
        
        public Dictionary<string, List<FilterResult>> GetAnalyzer(Dictionary<int, string> HTMLList, IHTMLAnalyzer HTMLAnalyzer)
        {
            Dictionary<string, List<FilterResult>> analyzerResult = new Dictionary<string, List<FilterResult>>();
            
            foreach (var html in HTMLList)
            {
                HTMLAnalyzer.SetInputHTML(html.Value);
                List<IHTMLFilter> filter = HTMLAnalyzer.Convert();

                foreach (var result in filter)
                {
                    if (!analyzerResult.ContainsKey(result.OutputKey))
                    {
                        analyzerResult.Add(result.OutputKey,  result.FilterHTML());
                    }
                    else
                    {
                        var fs=result.FilterHTML();
                        if (fs != null)
                        {
                            foreach (var f in fs)
                            {
                                analyzerResult[result.OutputKey].Add(f);
                            }
                        }
                    }
                }
            }

            return analyzerResult;
        }
        
        public Dictionary<string, List<FilterResult>> GetAnalyzer(string html, IHTMLAnalyzer HTMLAnalyzer)
        {
            Dictionary<string, List<FilterResult>> analyzerResult = new Dictionary<string, List<FilterResult>>();


            HTMLAnalyzer.SetInputHTML(html);
            List<IHTMLFilter> filter = HTMLAnalyzer.Convert();

            foreach (var result in filter)
            {
                if (!analyzerResult.ContainsKey(result.OutputKey))
                {
                    List<FilterResult> lr=result.FilterHTML();
                    if (lr!=null)
                        analyzerResult.Add(result.OutputKey,lr );
                }
                else
                {
                    if (analyzerResult[result.OutputKey] == null)
                        analyzerResult[result.OutputKey] = new List<FilterResult>();
                    var fs = result.FilterHTML();
                    if (fs != null)
                    {
                        foreach (var f in fs)
                        {
                            analyzerResult[result.OutputKey].Add(f);
                        }
                    }
                }
            }


            return analyzerResult;
        }


        public HTMLPublisherBase()
        {
            _InitParams();
        }
        private void _InitParams()
        {
        }

        public virtual InnerFormBase GetEditForm()
        {
            return null;
        }

    }
}
